package com.alibaba.service;

import com.alibaba.bean.Rank;
import com.alibaba.bean.RankList;
import com.alibaba.bean.RankListData;
import com.alibaba.bean.RankResult;
import com.alibaba.mapper.RankMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RankService {

    @Autowired
    private RankMapper rankMapper;

    public RankResult rankPage(Rank rank) {
        RankResult rr = new RankResult();
        RankList rl = new RankList();
        RankListData rld = new RankListData();
        int ranklow = (rank.getPage() - 1) * 20;
        int rankhigh = (rank.getPage() * 20) - 1;
        List<RankListData> rldlist = (List<RankListData>) rankMapper.rankAll(ranklow,rankhigh);
        try {
            if (rldlist != null && rldlist.size() > 0) {
                rr.setErrmsg("排行成功");
                rr.setResult(true);
                int i;
                if (rank.getPage() == 1 && rldlist.size() <= 10) {
                    for (i=0;i < rldlist.size();i++){
                        rldlist.get(i).setTop(true);
                    }
                }
                if (rank.getPage() == 1 && rldlist.size() > 10) {
                    for (i=0;i < 10;i++){
                        rldlist.get(i).setTop(true);
                    }
                }
                rl.setRankList(rldlist);
                rr.setData(rl);
            } else {
                rr.setErrmsg("无数据");
                rr.setResult(false);
                rr.setData(rl);
            }
        } catch (Exception e) {
            rr.setErrmsg(e.getMessage());
            e.printStackTrace();
        }
        return rr;
    }

}
